import { Color, QUI_BaseElement, QUI_Button, QUI_Canvas, QUI_Container, QUI_DragDriection, QUI_ElementType, QUI_Panel_Scroll, Vector2 } from "../common.js"
import { GameGridPos, GameMap, GameMapCell } from "../resource/gameresource.js";
import { QUI_Line } from "./line.js";

export class QUI_Map extends QUI_BaseElement {
    private containerMap: QUI_Container;
    constructor() {
        super();
        this.localRect.SetAsFill();
        this.containerMap = new QUI_Container();
        this.containerMap._parent = this;
        this.containerMap.localRect.setByPosAndSize(0, 0, 100, 100);
    }
    GetElementType(): QUI_ElementType {
        return QUI_ElementType.Element_User;
    }
    map: GameMap
    pos: GameMapCell;
    LoadMap(map: GameMap): void {
        this.map = map;
        let grid_width = 100;
        let gird_distX = 25;
        let grid_height = 30;
        let grid_distY = 25;
        let width = map.width * grid_width + (map.width - 1) * gird_distX;
        let height = map.height * grid_height + (map.height - 1) * grid_distY;
        this.containerMap.localRect.setBySize(width, height);
        for (var key in map.grids) {
            let grid = map.grids[key];
            let button = new QUI_Button();
            button.Tag = grid;
            button.localRect.setByPosAndSize(
                grid.pos.gridx * grid_width + (grid.pos.gridx) * gird_distX,
                grid.pos.gridy * grid_height + (grid.pos.gridy) * grid_distY,
                grid_width, grid_height);
            button.SetText(grid.title);
            button.OnClick = () => {
                let xdist = Math.abs(this.pos.pos.gridx - grid.pos.gridx);
                let ydist = Math.abs(this.pos.pos.gridy - grid.pos.gridy);
                if (xdist > 1 || ydist > 1)
                    return;
                if (xdist + ydist == 0)
                    return;
                //周围八格才能点

                this.SetPos(grid);

            }
            this.containerMap.AddChild(button);
        }
        for (var key in map.lines) {
            let grid_width = 100;
            let gird_distX = 25;
            let grid_height = 30;
            let grid_distY = 25;

            let line = map.lines[key];

            let lineUI = new QUI_Line();
            if (line.posB.gridx == line.posA.gridx)//竖线
            {
                lineUI.localRect.setByPosAndSize(
                    (line.posA.gridx + 0.5) * grid_width + (line.posA.gridx) * gird_distX,
                    (line.posA.gridy + 1) * grid_height + (line.posA.gridy) * grid_distY,
                    gird_distX, grid_distY);
                lineUI.pointA = new Vector2(0, 0);
                lineUI.pointB = new Vector2(0, grid_distY);
            }
            else if (line.posB.gridy == line.posA.gridy)//横线
            {
                lineUI.localRect.setByPosAndSize(
                    (line.posA.gridx + 1) * grid_width + (line.posA.gridx) * gird_distX,
                    (line.posA.gridy + 0.5) * grid_height + (line.posA.gridy) * grid_distY,
                    gird_distX, grid_distY);
                lineUI.pointA = new Vector2(0, 0);
                lineUI.pointB = new Vector2(gird_distX, 0);
            }
            else if (line.posB.gridy > line.posA.gridy && line.posB.gridx > line.posA.gridx)//右下
            {
                lineUI.localRect.setByPosAndSize(
                    (line.posA.gridx + 1) * grid_width + (line.posA.gridx) * gird_distX,
                    (line.posA.gridy + 1) * grid_height + (line.posA.gridy) * grid_distY,
                    gird_distX, grid_distY);
                lineUI.pointA = new Vector2(0, 0);
                lineUI.pointB = new Vector2(gird_distX, grid_distY);
            }
            else {
                lineUI.localRect.setByPosAndSize(
                    (line.posA.gridx) * grid_width + (line.posA.gridx) * gird_distX,
                    (line.posA.gridy + 1) * grid_height + (line.posA.gridy) * grid_distY,
                    gird_distX, grid_distY);
                lineUI.pointA = new Vector2(0, 0);
                lineUI.pointB = new Vector2(-gird_distX, grid_distY);
            }
            this.containerMap.AddChild(lineUI);
        }
    }
    SetPos(grid: GameMapCell): void {
        this.pos = grid;
        let grid_width = 100;
        let gird_distX = 25;
        let grid_height = 30;
        let grid_distY = 25;
        let centerx = (grid.pos.gridx + 0.5) * grid_width + (grid.pos.gridx) * gird_distX;
        let centery = (grid.pos.gridy + 0.5) * grid_height + (grid.pos.gridy) * grid_distY;
        let rect = this.GetWorldRect();
        this.containerMap.localRect.offsetX1 = rect.Width / 2 - centerx;
        this.containerMap.localRect.offsetY1 = rect.Height / 2 - centery;
    }
    OnRender(canvas: QUI_Canvas): void {
        let rect = this.GetWorldRect();
        canvas.batcherUI.EndDraw();

        //canvas.target.PushLimitRect(rect);
        canvas.batcherUI.ResumeDraw();
        this.containerMap.OnRender(canvas);
        canvas.batcherUI.EndDraw();
        //canvas.target.PopLimitRect();

        canvas.batcherUI.ResumeDraw();
    }
    OnUpdate(canvas: QUI_Canvas, delta: number): void {
        super.OnUpdate(canvas, delta);
        for (var i = 0; i < this.containerMap.GetChildCount(); i++) {
            let e = this.containerMap.GetChild(i);
            if (e.GetElementType() == QUI_ElementType.Element_Button) {
                let btn = this.containerMap.GetChild(i) as QUI_Button;
                let cell = btn.Tag as GameMapCell;
                let xdist = Math.abs(this.pos.pos.gridx - cell.pos.gridx);
                let ydist = Math.abs(this.pos.pos.gridy - cell.pos.gridy);

                if (Math.max(xdist, ydist) == 1) {
                    btn.colorNormal = Color.White;
                }
                else if (xdist == 0 && ydist == 0) {
                    btn.colorNormal = new Color(1, 1, 0, 1);
                }
                else {
                    btn.colorNormal = new Color(0.5, 0.5, 0.5, 1);
                }
            }
            else {

            }
        }
        this.containerMap.OnUpdate(canvas, delta);
    }
    OnTouch(canvas: QUI_Canvas, touchid: number, press: boolean, move: boolean, x: number, y: number): boolean {
        if (this.containerMap.OnTouch(canvas, touchid, press, move, x, y)) {
            return true;
        }
        return false;
    }
}
